package org.gdstash.ui.select;

import org.gdstash.db.DBItemSet;
import org.gdstash.db.SelectionCriteria;
import org.gdstash.ui.GDStashFrame;
import org.gdstash.ui.util.AdjustablePanel;
import org.gdstash.util.GDMsgFormatter;

import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;
import java.awt.*;
import java.util.List;

public class ItemSetPane extends AdjustablePanel {
    private static List<DBItemSet> sets;
    private static DBItemSet[] itemSets;
    private JComboBox<DBItemSet> cbSets;

    static {
        ItemSetPane.sets = null;
        ItemSetPane.itemSets = null;
        loadSets();
    }

    public ItemSetPane() {
        this.adjustUI();
        GroupLayout layout = null;
        GroupLayout.SequentialGroup hGroup = null;
        GroupLayout.SequentialGroup vGroup = null;
        layout = new GroupLayout(this);
        this.setLayout(layout);
        layout.setAutoCreateGaps(false);
        layout.setAutoCreateContainerGaps(false);
        hGroup = layout.createSequentialGroup();
        hGroup.addGroup(layout.createParallelGroup().addComponent(this.cbSets));
        layout.setHorizontalGroup(hGroup);
        vGroup = layout.createSequentialGroup();
        vGroup.addGroup(layout.createParallelGroup().addComponent(this.cbSets));
        layout.setVerticalGroup(vGroup);
    }

    @Override
    public void adjustUI() {
        Font fntLabel = UIManager.getDefaults().getFont("Label.font");
        Font fntCombo = UIManager.getDefaults().getFont("ComboBox.font");
        if (fntCombo == null) {
            fntCombo = fntLabel;
        }
        Font fntBorder = UIManager.getDefaults().getFont("TitledBorder.font");
        if (fntBorder == null) {
            fntBorder = fntLabel;
        }
        fntLabel = fntLabel.deriveFont((float) GDStashFrame.iniConfig.sectUI.fontSize);
        fntCombo = fntCombo.deriveFont((float) GDStashFrame.iniConfig.sectUI.fontSize);
        fntBorder = fntBorder.deriveFont((float) GDStashFrame.iniConfig.sectUI.fontSize);
        final Border lowered = BorderFactory.createEtchedBorder(1);
        final Border raised = BorderFactory.createEtchedBorder(0);
        final Border compound = BorderFactory.createCompoundBorder(raised, lowered);
        final TitledBorder text = BorderFactory.createTitledBorder(compound, GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_SETS"));
        text.setTitleFont(fntBorder);
        this.setBorder(text);
        if (this.cbSets == null) {
            this.cbSets = new JComboBox<DBItemSet>(ItemSetPane.itemSets);
        }
        this.cbSets.setFont(fntCombo);
    }

    public void updateItemSets() {
        final DefaultComboBoxModel<DBItemSet> model = new DefaultComboBoxModel<DBItemSet>(ItemSetPane.itemSets);
        this.cbSets.setModel(model);
    }

    public void clear() {
        this.cbSets.getModel().setSelectedItem(null);
    }

    public void addCriteria(final SelectionCriteria criteria) {
        criteria.setItem = false;
        final int index = this.cbSets.getSelectedIndex();
        if (index == 1) {
            criteria.setItem = true;
        }
        if (index > 1) {
            final DBItemSet set = (DBItemSet) this.cbSets.getSelectedItem();
            if (set != null) {
                criteria.itemIDs = set.getItemIDList();
            } else {
                criteria.itemIDs = null;
            }
        }
    }

    public static void loadSets() {
        if (GDStashFrame.dbConfig == null || !GDStashFrame.dbConfig.gddbInit) {
            (ItemSetPane.itemSets = new DBItemSet[1])[0] = null;
            return;
        }
        ItemSetPane.sets = DBItemSet.getAll();
        if (ItemSetPane.sets == null) {
            (ItemSetPane.itemSets = new DBItemSet[1])[0] = null;
        } else {
            (ItemSetPane.itemSets = new DBItemSet[ItemSetPane.sets.size() + 2])[0] = null;
            ItemSetPane.itemSets[1] = new DBItemSet(GDMsgFormatter.getString(GDMsgFormatter.rbUI, "TXT_ITEMSET_ALL"));
            int pos = 2;
            for (final DBItemSet set : ItemSetPane.sets) {
                ItemSetPane.itemSets[pos] = set;
                ++pos;
            }
        }
    }
}
